using System.Text.RegularExpressions;

namespace CSharpPro._1028;

public class UserServiceImpl : UserService
{
    // 使用泛型集合存储用户信息
    private readonly List<User> _users = new List<User>();

    public void ShowMainMenu()
    {
        bool isRunning = true;

        while (isRunning)
        {
            Console.Clear();
            Console.WriteLine("====== 用户系统 ======");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.Write("请选择功能：");

            string choice = Console.ReadLine()?.Trim();

            switch (choice)
            {
                case "1":
                    Login();
                    break;
                case "2":
                    Regist();
                    break;
                case "3":
                    isRunning = false;
                    Console.WriteLine("成功退出");
                    break;
                default:
                    Console.WriteLine("请输入有效数字 (1-3)");
                    WaitForKeyPress();
                    break;
            }
        }
    }

    public bool Login()
    {
        Console.Clear();
        Console.WriteLine("===== 用户登录 =====");

        Console.Write("请输入用户名：");
        string username = Console.ReadLine()?.Trim() ?? "";
        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不可为空");
            WaitForKeyPress();
            return false;
        }

        Console.Write("请输入密码：");
        string password = Console.ReadLine()?.Trim() ?? "";
        if (string.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不可为空");
            WaitForKeyPress();
            return false;
        }

        bool isFound = false;
        foreach (var user in _users)
        {
            if (user.Username == username)
            {
                isFound = true;
                if (user.Password == password)
                {
                    Console.WriteLine("\n登录成功！欢迎回来，" + username + "！");
                    return true;
                }

                break;
            }
        }

        if (!isFound)
        {
            Console.WriteLine("错误：用户名不存在");
        }
        else
        {
            Console.WriteLine("错误：密码错误");
        }

        WaitForKeyPress();
        return false;
    }

    public bool Regist()
    {
        Console.Clear();
        Console.WriteLine("===== 用户注册 =====");

        string username;
        while (true)
        {
            Console.Write("输入用户名：");
            username = Console.ReadLine()?.Trim() ?? "";

            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("用户名不可为空");
                continue;
            }

            if (!IsValidUsername(username))
            {
                Console.WriteLine("用户名无效！长度必须为3-15个字符，且只能包含字母、数字和下划线");
                continue;
            }

            bool isExist = false;
            foreach (var user in _users)
            {
                if (user.Username == username)
                {
                    isExist = true;
                    break;
                }
            }

            if (isExist)
            {
                Console.WriteLine("用户名已存在，请选择其他用户名");
                continue;
            }

            break;
        }

        string password;
        while (true)
        {
            Console.Write("请输入密码：");
            password = Console.ReadLine()?.Trim() ?? "";

            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("密码不可为空");
                continue;
            }

            if (!IsValidPassword(password))
            {
                Console.WriteLine("密码无效！长度必须为6-20个字符，且必须包含至少1个大写字母、1个小写字母和1个数字");
                continue;
            }

            break;
        }

        while (true)
        {
            Console.Write("请确认密码：");
            string confirmPassword = Console.ReadLine()?.Trim() ?? "";

            if (string.IsNullOrEmpty(confirmPassword))
            {
                Console.WriteLine("确认密码不可为空");
                continue;
            }

            if (confirmPassword != password)
            {
                Console.WriteLine("两次输入的密码不一致，请重新输入");
                continue;
            }

            break;
        }

        _users.Add(new User(username, password));
        Console.WriteLine("\n注册成功！");
        WaitForKeyPress();
        return true;
    }

    private bool IsValidUsername(string username)
    {
        if (username.Length < 3 || username.Length > 15)
            return false;
        return Regex.IsMatch(username, @"^[a-zA-Z0-9_]*$");
    }

    private bool IsValidPassword(string password)
    {
        if (password.Length < 6 || password.Length > 20)
            return false;

        bool hasUpper = Regex.IsMatch(password, @"[A-Z]");
        bool hasLower = Regex.IsMatch(password, @"[a-z]");
        bool hasDigit = Regex.IsMatch(password, @"[0-9]");

        return hasUpper && hasLower && hasDigit;
    }

    private void WaitForKeyPress()
    {
        Console.WriteLine("\n按任意键继续...");
        Console.ReadKey();
    }
}